<include file="header"/>
<include file="loadding"/>
<script type="text/javascript">
    var flag = 0;
    var userType = "{BEESCRM:$userType}";
</script>
<body>
<div class="wraper wraper-btm">
    <div class="head clearfix">
        <a href="javascript:history.go(-1);" class="iconfont icon-fanhui72 pull-left"></a>
        <span class="head-name pull-left">订桶装水</span>
        <a href="{BEESCRM::U('base/water/orderList',array('water_id' => $water_id))}" class="pull-left text-right"><span
                class="iconfont icon-lishi"></span> <span class="lishi">订水记录</span></a>
    </div>
    <div class="contain">
        <p class="field">派送地址</p>

        <p class="form-list">
            <input type="text" placeholder="姓名" value="{BEESCRM:$lastOrder.username|default=$user['real_name']}"
                   id="username" style="border: none;width: 100%;font-size: 14px;">
        </p>

        <p class="form-list">
            <input type="text" placeholder="手机" value="{BEESCRM:$lastOrder.mobile|default=$user['mobile']}" id="mobile"
                   style="border: none;width: 100%;font-size: 14px;">
        </p>
        <if condition="($userType eq 0) OR ($water['teacher'] eq 1)">
            <p class="form-list">
                <select class="form-control" name="community_id" id="searchCommunityId"
                        value="{BEESCRM:$lastOrder.community_id}" style="border-bottom:1px solid #ddd">
                    {BEESCRM:$addrData.community}
                </select>
                <select class="form-control" name="building_id" id="searchBuildingId"
                        value="{BEESCRM:$lastOrder.building_id}" style="border-bottom:1px solid #ddd">
                    <if condition="$addrData['building_id'] lt 1">
                        <option value="0">请选择楼幢</option>
                    </if>
                    {BEESCRM:$addrData.building}
                </select>
                <select class="form-control" name="floor_id" id="searchFloorId" value="{BEESCRM:$lastOrder.floor_id}"
                        style="border-bottom:1px solid #ddd">
                    <if condition="$addrData['floor_id'] lt 1">
                        <option value="0">请选择楼层</option>
                    </if>
                    {BEESCRM:$addrData.floor}
                </select>
                <select class="form-control" name="room_id" id="searchRoomId" value="{BEESCRM:$lastOrder.room_id}">
                    <if condition="$addrData['room_id'] lt 1">
                        <option value="0">请选择房间号</option>
                    </if>
                    {BEESCRM:$addrData.room}
                </select>
            </p>
            <else/>
            <p class="form-list">
                <input type="text" placeholder="地址" value="{BEESCRM:$lastOrder.address|default=$user['address']}"
                       id="address" style="border: none;width: 100%;font-size: 14px;">
            </p>
        </if>
    </div>
    <div class="contain">
        <p class="field">桶装水选择</p>

        <p class="form-list">
            <select id="goods">
                <option value="0" data-price="0">请选择桶装水</option>
                <volist name="goods" id="vo">
                    <option value="{BEESCRM:$vo.goods_id}" data-price="{BEESCRM:$vo.price}">{BEESCRM:$vo.goods_name}
                    </option>
                </volist>
            </select>
            <span class="triangle"></span>
        </p>

        <!-- <div class="lists">
            购买数量<span class="pull-right">1</span>
        </div> -->
        <!-- <input type="hidden" value="{BEESCRM:$water.order_limit}" id="buy-num"/> -->
        <div class="quantity-box clearfix">
            <p class="pull-left">购买数量</p>

            <div class="quantity pull-right">
                <span class="iconfont icon-iconfont55 change-num-btn jian" data-id="0" data-status="sub"></span>
                <input class="buy-count buy-count-0" type="number" value="1" id="buy-num" data-id="0"
                       data-status="input" readonly>
                <span class="iconfont icon-jia change-num-btn jia" data-id="0" data-status="plus"></span>
            </div>
        </div>
        <!-- <p class="lists price text-right">总计:<span class="total-price">0元</span></p> -->
    </div>
    <if condition="$snack">
    <div class="contain snack-contain">
        <p class="field snack-show">零食选择<span class="icon-you iconfont pull-right"></span></p>

        <div class="snack">
                <volist name="snack" id="vo">
                    <div class="quantity-box clearfix snack-money">
                        <p class="pull-left">{BEESCRM:$vo.snack_name}
                            <span class="text-red" data-price="{BEESCRM:$vo.price}">￥{BEESCRM:$vo.price}</span>
                        </p>
                        <div class="quantity pull-right" id="snack_{BEESCRM:$vo.snack_id}">
                            <span class="iconfont icon-iconfont55 change-count-btn sub" data-id="{BEESCRM:$vo.snack_id}"
                                  data-status="sub"></span>
                            <input class="snack-count buy-count-{BEESCRM:$vo.snack_id}" type="number" name="number"
                                   value="0" data-id="{BEESCRM:$vo.snack_id}" data-price="{BEESCRM:$vo.price}" data-status="input" readonly>
                            <span class="iconfont icon-jia change-count-btn plus" data-id="{BEESCRM:$vo.snack_id}"
                                  data-status="plus"></span>
                        </div>
                    </div>
                </volist>
        </div>
    </div>
        </if>
    <div class="contain">
        <p class="field">优惠券</p>
        {BEESCRM::W('Coupon',array('module_id'=>8,'account_id' => $account_id,'user_id' => $user_id))}
    </div>
    <if condition="$water['content']">
        <div class="contain">
            <p class="field"><span style="color: red;">{BEESCRM:$water.content}</span></p>
        </div>
    </if>

    <div class="btn-box">
        <a href="javascript:void(0);" class="btn btn-confirm">确认</a>
    </div>
</div>
{BEESCRM::W('GetCoupon',array('module_id'=>8,'account_id' => $account_id,'user_id' => $user_id))}
<include file="commonJs"/>
<script>
    $('.snack-show').click(function () {
        $(this).parent('.snack-contain').find('.icon-you').toggleClass('show-more');
        $(this).parent('.snack-contain').find('.snack').slideToggle();
    })

    /*//数量控制
    $('.change-count-btn').click(function () {
        var id = $(this).data('id');
        var status = $(this).data('status');
        var num = $(this).parent().find('input').val();
        renderNumVal(id, status, num);
    });

    $('.snack-count').change(function () {
        var id = $(this).data('id');
        var status = $(this).data('status');
        var num = $(this).val();
        renderNumVal(id, status, num);
    });

    function renderNumVal(id, status, num) {
        if (status == 'plus') {
            num++;
        } else if (status == 'sub') {
            if (num < 1) {
                num = 0;
                return false;
            }
            num--;
        } else if (status == 'input') {
            num = $('.buy-count-' + id + '').val();
            if (num <= 1) {
                num = 1;
                $('.buy-count-' + id + '').val(num);
            }
        }
        $('.buy-count-' + id + '').val(num);
    }*/
</script>
<script type="text/javascript">
    //$('.circle-loadding').show();
    var swiper = new Swiper('.banner .swiper-container', {
        pagination: '.banner .swiper-pagination',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });

    var lim = "{BEESCRM:$water.order_limit}";
    lim = parseInt(lim);
    var lim_one = "{BEESCRM:$water.order_one_limit}";
    lim_one = parseInt(lim_one);

    $(function () {
        var money = $('#goods').find("option:selected").data('price');
        if (money == undefined) {
            money = 0;
        }
        $('.total-price').text(money + '元');

        var cId = $('#searchCommunityId').attr('value');
        if (cId) {
            $("#searchCommunityId option[value=" + cId + "]").prop('selected', 'true');
            getBuilding(0);
        }

    });

    // 选择水
    $(document).on('change', '#goods', function () {
        var money = getMoney();
        // 总价
        $('#old_money').val(money);
        //$('#buy-num').val(1);
        // 优惠券
        coupon_change_money();
    });

    // 水
    $('.jian').on('click', function () {
        var val = $('#buy-num').val();
        if (val > 1) {
            val = parseInt(val) - 1;
            //money = money * parseInt(val);
        }
        $('#buy-num').val(val);

        var money = getMoney();
        $('#old_money').val(money);

        coupon_change_money();
        //$('.total-price').text('￥'+money+'元');
    });

    $('.jia').on('click', function () {
        var val = $('#buy-num').val();
        if (lim_one == 0) {
            val = parseInt(val) + 1;
        }
        else if (val < lim_one) {
            val = parseInt(val) + 1;
            //money = money * parseInt(val);
        }
        $('#buy-num').val(val);

        var money = getMoney();
        $('#old_money').val(money);
        coupon_change_money();
        //$('.total-price').text('￥'+money+'元');
    });

    // 零食减
    $('.sub').on('click', function () {
        var id = $(this).data('id');
        var obj = "#snack_"+id+" .snack-count";
        var val = $(obj).val();
        if (val >= 1) {
            val = parseInt(val) - 1;
        }
        $(obj).val(val);

        var money = getMoney();
        $('#old_money').val(money);
        coupon_change_money();
    });

    // 零食加
    $('.plus').on('click', function () {
        var id = $(this).data('id');
        var obj = "#snack_"+id+" .snack-count";
        var val = $(obj).val();
        val = parseInt(val) + 1;
        $(obj).val(val);

        var money = getMoney();
        $('#old_money').val(money);
        coupon_change_money();
    });

    //判断是否已经有人购买过
    $(document).on('click', '.btn-confirm', function () {
        $.ajax({
            'url': "{BEESCRM::U('/base/water/getLastOrder', array('water_id' => $water_id))}",
            'dataType': 'json',
            'type': 'get',
            beforeSend: function () {
                $('.circle-loadding').show();
            },
            success: function (data) {
                $('.circle-loadding').hide();
                if (data.status == 'F') {
                    $.confirm(data.message, postdata);
                } else {
                    postdata();
                }
            }
        });
    });

    function postdata() {
        var goods_id = $('#goods').val();
        var goods_num = $('#buy-num').val();
        goods_num = parseInt(goods_num);
        var arr = new Array();
        var i = 0;
        $('.snack-money .snack-count').each(function(){
            var snack_num = $(this).val();
            snack_num = parseInt(snack_num);
            var snack_id = $(this).data('id');
            if(snack_num > 0){
                arr[i] = {snack_id:snack_id,snack_num:snack_num};
                //arr. push(snack_id);
                //arr. push(snack_num);
                i++;
            }
        });
        var snack_arr = JSON.stringify(arr);
        //alert(snack_arr);
        var username = $('#username').val();
        var mobile = $('#mobile').val();
        var address = $('#address').val();
        var community_id = $('#searchCommunityId').val();
        var building_id = $('#searchBuildingId').val();
        var floor_id = $('#searchFloorId').val();
        var room_id = $('#searchRoomId').val();
        var record_id = $('#mycoupon').val();
        var userType = "{BEESCRM:$userType}";
        var water_teacher = "{BEESCRM:$water['teacher']}";
        var order_amount = $('#old_money').val();

        if (!username) {
            $.alert('error', '请填写姓名');
            return false;
        }
        if (!mobile || mobile == 0) {
            $.alert('error', '请填写电话');
            return false;
        }
        if (userType > 0 && water_teacher!=1) {
            if (!address) {
                $.alert('error', '请填写详细地址');
                return false;
            }
        }
        else {
            if (!community_id || !building_id || !floor_id || !room_id) {
                $.alert('error', '请选择完整地址');
                return false;
            }
        }
        if (!goods_id) {
            $.alert('error', '请选择桶装水');
            return false;
        }
        if (goods_num > lim) {
            $.alert('error', '每人每天只能购买' + lim + '桶水');
            return false;
        }

        var info = {};
        info.goods_id = goods_id;
        info.goods_num = goods_num;
        info.address = address;
        info.community_id = community_id;
        info.building_id = building_id;
        info.floor_id = floor_id;
        info.room_id = room_id;
        info.username = username;
        info.mobile = mobile;
        info.record_id = record_id;
        info.snack_arr = snack_arr;
        info.order_amount = order_amount;
        $.ajax({
            'url': "{BEESCRM::U('/base/water/doSubmitOrder', array('water_id' => $water_id))}",
            'data': info,
            'dataType': 'json',
            'type': 'POST',
            //'traditional': true,
            beforeSend: function () {
                $('.circle-loadding').show();
            },
            success: function (data) {
                $('.circle-loadding').hide();
                if (data.status == 'S') {
                    window.location.href = data.url;
                } else {
                    $.alert('error', data.message);
                    return false;
                }
            }
        });
    }


    //查询获得楼幢
    $(document).on('change', '#searchCommunityId', function () {
        getBuilding(0);
    })
    //查询获得楼层
    $(document).on('change', '#searchBuildingId', function () {
        getFloor(0);
    })
    //查询获得房间号
    $(document).on('change', '#searchFloorId', function () {
        getRoom(0);
    })

    function getBuilding(type) {
        if (type == 1) {
            var communityId = $('#communityId').val();
        } else {
            var communityId = $('#searchCommunityId').val();
        }
        var data = {};
        var json = {};
        data.communityId = communityId;
        data.buildingId = 0;
        if (data.communityId > 0) {
            json.data = data;
            json.url = "/base/water/getBuildingOption/water_id/{BEESCRM:$water_id}";
            json.method = 'post';
            json.type = 'json';
            if (type == 1) {
                $.getAjax(json, false, buildingOption, '');
            } else {
                $.getAjax(json, false, searchBuildingOption, '');
            }
        } else {
            if (type == 1) {
                $('#buildingId').html('<option value="0" selected="selected">请选择楼幢</option>');
            } else {
                $('#searchBuildingId').html('<option value="0" selected="selected">请选择楼幢</option>');
            }
        }
    }
    function buildingOption(data) {
        $('#buildingId').html(data.data);
    }
    function searchBuildingOption(data) {
        $('#searchBuildingId').html(data.data);
        var bId = $('#searchBuildingId').attr('value');
        if (bId) {
            $("#searchBuildingId option[value=" + bId + "]").prop('selected', 'true');
            getFloor(0);
        }
    }

    function getFloor(type) {
        if (type == 1) {
            var buildingId = $('#buildingId').val();
        } else {
            var buildingId = $('#searchBuildingId').val();
        }
        var data = {};
        var json = {};
        data.buildingId = buildingId;
        data.floorId = 0;
        if (data.buildingId > 0) {
            json.data = data;
            json.url = '/base/water/getFloorOption/water_id/{BEESCRM:$water_id}';
            json.method = 'post';
            json.type = 'json';
            if (type == 1) {
                $.getAjax(json, false, floorOption, '');
            } else {
                $.getAjax(json, false, searchFloorOption, '');
            }
        } else {
            if (type == 1) {
                $('#floorId').html('<option value="0" selected="selected">请选择楼层</option>');
            } else {
                $('#searchFloorId').html('<option value="0" selected="selected">请选择楼层</option>');
            }
        }
    }
    function floorOption(data) {
        $('#floorId').html(data.data);
    }
    function searchFloorOption(data) {
        $('#searchFloorId').html(data.data);
        var fId = $('#searchFloorId').attr('value');
        if (fId) {
            $("#searchFloorId option[value=" + fId + "]").prop('selected', 'true');
            getRoom(0);
        }
    }

    function getRoom(type) {
        if (type == 1) {
            var floorId = $('#floorId').val();
        } else {
            var floorId = $('#searchFloorId').val();
        }
        var data = {};
        var json = {};
        data.floorId = floorId;
        data.roomId = 0;
        if (data.floorId > 0) {
            json.data = data;
            json.url = '/base/water/getRoomOption/water_id/{BEESCRM:$water_id}';
            json.method = 'post';
            json.type = 'json';
            if (type == 1) {
                $.getAjax(json, false, roomOption, '');
            } else {
                $.getAjax(json, false, searchRoomOption, '');
            }
        } else {
            if (type == 1) {
                $('#roomId').html('<option value="0" selected="selected">请选择房间号</option>');
            } else {
                $('#searchRoomId').html('<option value="0" selected="selected">请选择房间号</option>');
            }
        }
    }
    function roomOption(data) {
        $('#roomId').html(data.data);
    }
    function searchRoomOption(data) {
        $('#searchRoomId').html(data.data);
        var rId = $('#searchRoomId').attr('value');
        if (rId) {
            $("#searchRoomId option[value=" + rId + "]").prop('selected', 'true');
        }
    }

    // 获取总价
    function getMoney() {
        // 获取水总价
        var water_num = $('#buy-num').val();
        var water_price = $('#goods').find("option:selected").data('price');
        var water_money = parseFloat(water_price) * parseInt(water_num);
        // 获取零食总价
        var snack_money = 0;
        $('.snack-money .snack-count').each(function(){
            var snack_num = $(this).val();
            var snack_price = $(this).data('price');
            snack_money += parseInt(snack_num)*parseFloat(snack_price);
        });
        price =  snack_money + water_money;
        return price.toFixed(2); // 保留两位小数
    }


</script>
</body>
</html>
